

export function pagesCode(basicsData, crudData) {
  // 基础json数据
  let basicsJsonData = {};
  JSON.parse(JSON.stringify(basicsData)).forEach((element) => {
    basicsJsonData[element.columnName] = element;
  });
  // crud json数据
  let crudJsonData = {};
  JSON.parse(JSON.stringify(crudData)).forEach((element) => {
    crudJsonData[element.value] = element;
  });

  // });
  let tableData = table(crudJsonData.table.fieldData, basicsJsonData);
  let createData = create(crudJsonData.create.fieldData, basicsJsonData);
  let QueryData = Query(crudJsonData.search.fieldData, basicsJsonData);
  let EditData = Edit(crudJsonData.edit.fieldData, basicsJsonData);
  let DetailData = Detail(crudJsonData.detail.fieldData, basicsJsonData);

  let ListCode = ``;
  let createCode = ``;
  let QueryCode = ``;
  let EditCode = ``;
  let DetailCode = ``;
 
  let dataReturn = dataReturnCode(crudJsonData, basicsData);



  let filtersCode = pagesFilters(basicsJsonData, crudData);

  if (tableData) {
    ListCode = `
      <!-- 表格数据 -->
      <a-table :data-source="tableData" rowKey="id" :pagination="false">
  ${tableData}
  <a-table-column title="操作"
        ><template slot-scope="text, record">
          <a @click="handleUpdate(record)">修改</a>
          <a-divider type="vertical" />
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical" />
          <a href="#" @click="handleDelete(record)">删除</a>
        </template>
      </a-table-column>
  </a-table>
  <!-- 分页区域 -->
  <a-pagination size="small" :total="total" show-size-changer show-quick-jumper />
      `;
  }

  if (createData) {
    createCode = `
      <!-- 添加对话框 -->
      <a-modal
      v-model="createOpen"
      title="修改"
      ok-text="确认"
      cancel-text="取消"
      @ok="createSubmit"
    >
      <a-form-model
      ref="createForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
  ${createData}
  </a-form-model>
  </a-modal>
      `;
  }

  if (QueryData) {
    QueryCode = `
      <!-- 检索对话框 -->
      <a-form-model
      layout="inline"
      :model="queryParams"
    >
  ${QueryData}
  <a-form-model-item>
  <a-space>
    <a-button type="primary"> 搜索 </a-button>
    <a-button>重置</a-button>
  </a-space>
</a-form-model-item>
  </a-form-model>
   
      `;
  }

  if (DetailData) {
    DetailCode = `
      <!-- 详情对话框 -->
      <a-modal
      v-model="detailOpen"
      title="修改"
      ok-text="确认"
      cancel-text="取消"
      @ok="detailOpen"
    >
      <a-form-model
      ref="detailForm"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
  ${DetailData}
  </a-form-model>
  </a-modal>
      `;
  }

  if (EditData) {
    EditCode = `
      <!-- 修改对话框 -->
      <a-modal
      v-model="editOpen"
      title="修改"
      ok-text="确认"
      cancel-text="取消"
      @ok="editSubmit"
    >
      <a-form-model
      ref="queryParams"
      :model="form"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
  ${EditData}
  </a-form-model>
  </a-modal>
      `;
  }

  return `
    <template>
   <div class="warp">
  ${QueryCode}
  ${ListCode}
  ${createCode}
  ${EditCode}
  ${DetailCode}
  
   </div>
  </template>

  <script>
  export default {
  data() {
  return {
    \/\/总页数
    total:0,
    labelCol: { span: 4 },
    wrapperCol: { span: 14 },
    form:{},
    tableData:[{
      id:1,
      name:"刘双龙",
      profession:"2",
      age:24
   }],
    queryParams:{},
  ${dataReturn},
  editOpen:false,
  detailOpen:false,
  createOpen:false
  };
  },
  filters: {
    professionOptions(a, b) {
      return b.filter((item) => item.value === a)[0].label;
    },
  },
  methods: {
    editSubmit() {
      console.log("debug log --> ", this.form);
    },
    detailSubmit() {
      console.log("debug log --> ", this.form);
    },
    createSubmit() {
      console.log("debug log --> ", this.form);
    },
    handleUpdate(data) {
      this.form = data;
      this.editOpen = true;
    },
    handleDetail(data) {},
    handleDelete(data) {
      this.form = data;
      this.detailOpen = true;
    },
  }
  };
  </script>
  `;
}
// 创建
function create(crudData, basicsDta) {

  let create = crudData.filter((item) => item.isAdopt);
  let code = "";

  create.forEach((element) => {

    code += formCodePage(element, basicsDta, 'create');
  });

  return code;
}
// 列表
function table(crudData, basicsDta) {
  let crud = crudData.filter((item) => item.isAdopt);
  let code = `
  `;
  crud.forEach((element) => {
    code += tableCodePage(element, basicsDta);
  });
  return code;
}
// 查询
function Query(crudData, basicsDta) {
  let query = crudData.filter((item) => item.isAdopt);
  let code = "";
  query.forEach((element) => {
    code += formCodePage(element, basicsDta, 'query');
  });
  return code;
}
// 查看
function Detail(crudData, basicsDta) {
  let detail = crudData.filter((item) => item.isAdopt);
  let code = "";
  detail.forEach((element) => {
    code += formCodePage(element, basicsDta, 'detail');
  });
  return code;
}
// 编辑
function Edit(crudData, basicsDta) {
  let edit = crudData.filter((item) => item.isAdopt);
  let code = "";
  edit.forEach((element) => {
    code += formCodePage(element, basicsDta, 'edit');
  });
  return code;
}

function tableCodePage(data, basicsDta) {
  if (
    basicsDta[data.value].fieldType === "input" ||
    basicsDta[data.value].fieldType === "textarea" ||
    basicsDta[data.value].fieldType === "number"
  ) {
    return `<a-table-column title="${basicsDta[data.value].label}" data-index="${data.value}" />`;
  }

  if (
    basicsDta[data.value].fieldType === "select" ||
    basicsDta[data.value].fieldType === "radio" ||
    basicsDta[data.value].fieldType === "checkbox"
  ) {
    return `<a-table-column title="${basicsDta[data.value].label}" data-index="${data.value}"><template slot-scope="text"> {{text | ${data.value}OptionsFilter(${data.value}Options)}} </template> </a-table-column>`;
  }

  if (
    basicsDta[data.value].fieldType === "date" ||
    basicsDta[data.value].fieldType === "time"
  ) {
    return `<a-table-column title="${basicsDta[data.value].label}" data-index="${data.value}"><template slot-scope="text">{{text | ${basicsDta[data.value].fieldType}Filter}}</template></a-table-column>`;
  }
}

function formCodePage(data, basicsDta, sign) {
  if (basicsDta[data.value].fieldType === "input") {
    return `<a-form-model-item  label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}><a-input v-model="${sign === "query" ? 'queryParams' : 'form'}.${data.value}" placeholder="请选择${basicsDta[data.value].label}"/></a-form-model-item>`;
  }

  if (basicsDta[data.value].fieldType === "number") {
    return `<a-form-model-item   label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}>
    <a-input-number v-model=" ${sign === "query" ? 'queryParams' : 'form'}.${data.value}"  />
  </a-form-model-item>`;
  }

  if (basicsDta[data.value].fieldType === "select") {
    return `<a-form-model-item   label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}><a-select v-model=" ${sign === "query" ? 'queryParams' : 'form'}.${data.value}" placeholder="请选择${basicsDta[data.value].label}" :options="${data.value}Options"></a-select></a-form-model-item>`;
  }

  if (basicsDta[data.value].fieldType === "radio") {
    return `<a-form-model-item   label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}><a-radio-group :options="${data.value}Options" v-model=" ${sign === "query" ? 'queryParams' : 'form'}.${data.value}"></a-radio-group></a-form-model-item>`;
  }

  if (basicsDta[data.value].fieldType === "checkbox") {
    return `<a-form-model-item   label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}><a-checkbox-group v-model=" ${sign === "query" ? 'queryParams' : 'form'}.${data.value}" :options="${data.value}Options" /></a-form-model-item>`;
  }

  if (basicsDta[data.value].fieldType === "datetime") {
    return `<a-form-model-item   label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}><a-date-picker @change="${data.value}DateChange" /></a-form-model-item>`;
  }

  if (basicsDta[data.value].fieldType === "textarea") {
    return `<a-form-model-item   label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}><a-textarea v-model=" ${sign === "query" ? 'queryParams' : 'form'}.${data.value}" placeholder="请输入" auto-size /></a-form-model-item>`;
  }

  if (basicsDta[data.value].fieldType === "switch") {
    return `<a-form-model-item   label="${basicsDta[data.value].label}" ${basicsDta[data.value].isRequired && (sign === "edit" || sign === "create") ? "prop=" + '"' + basicsDta[data.value].columnName + '"' : ""}><a-switch  v-model=" ${sign === "query" ? 'queryParams' : 'form'}.${data.value}"/></a-form-model-item>`;
  }
}

function dataReturnCode(crudData, basicsDta) {
  let options = basicsDta.filter(item => item.fieldType === 'select')
  let rules = basicsDta.filter(item => item.isRequired)
  let code = ``
  // options数据生成
  options.forEach(element => {
    code += `${element.columnName}Options:${JSON.stringify(element.options)},`
  });
  // rules数据生成
  if (rules.length > 0) {
    let rulesData = {}
    rules.forEach(element => {
      if (element.fieldType === 'input' || element.fieldType === 'number' || element.fieldType === 'textarea') { rulesData[element.columnName] = [{ required: true, message: `请输入${element.label}`, trigger: 'blur' }] }

      if (element.fieldType === 'select' || element.fieldType === 'checkbox' || element.fieldType === 'radio' || element.fieldType === 'switch' || element.fieldType === 'date' || element.fieldType === 'time') { rulesData[element.columnName] = [{ required: true, message: `请选择${element.label}`, trigger: 'change' }] }

    });

    code += `rules:${JSON.stringify(rulesData)}`
  }
  return code
}


function pagesFilters(basicsJsonData, crudData){

  
}